//
//  CommentCellView.m
//  JiaoX
//
//  Created by yinqy on 15/1/9.
//  Copyright (c) 2015年 北京娇羞科技有限公司. All rights reserved.
//

#import "CommentCellView.h"
#import "ReComtView.h"
#import "JXPostsDetailCtl.h"
#import "JXNavigationController.h"
#import "JXLoginRegistCheckController.h"
#import "JXComtPopMenu.h"
#import "JXUserInfoCtl.h"
#import "JXMinePostsCtl.h"

#define SPLITE_COLOR JXColor(229, 229, 229)

@interface CommentCellView()<ReComtViewDelegate,JXComtPopMenuDelegate>
{
    UIImageView *_headImage;
    UILabel *_nameLabel;
    UIButton *_timeBtn;
    UIButton *_replyImageBtn;
    
    UIImageView *_genderView;
    UIImageView *_authorView;
    
    UIView *_spliteView1;
    UIView *_spliteView2;
    
    UILabel *_contentLabel;
    
    ReComtView *_reComtView;
    
    UIButton *_moreBtn;
    JXComtPopMenu *_menu;
}

@end
@implementation CommentCellView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        //1.添加头像
        _headImage = [[UIImageView alloc]initWithFrame:CGRectMake(JXPublicInset, JXPublicInset, HEADIMAGE_HEIGHT, HEADIMAGE_HEIGHT)];
        [_headImage.layer setCornerRadius:HEADIMAGE_HEIGHT*0.5];
        [_headImage.layer setMasksToBounds:YES];
        [self addSubview:_headImage];
        
        //2 添加回复图标
        _replyImageBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        UIImage *replyImage = [UIImage imageNamed:@"ic_comt_reply"];
        [_replyImageBtn setImage:replyImage forState:UIControlStateNormal];
        [_replyImageBtn setImage:[UIImage imageNamed:@"ic_comt_reply_heighlight"] forState:UIControlStateSelected];
        [_replyImageBtn setImage:[UIImage imageNamed:@"ic_comt_reply_heighlight"] forState:UIControlStateHighlighted];
        [self addSubview:_replyImageBtn];
        
        //3.添加名称
        _nameLabel = [[UILabel alloc]initWithFrame:CGRectMake(CGRectGetMaxX(_headImage.frame)+JXPublicInset*0.5, CGRectGetMinY(_headImage.frame)+JXPublicInset/4, JXScreenW-JXPublicInset*2-JXPublicInset-HEADIMAGE_HEIGHT, [UIFont systemFontOfSize:NAMELABEL_FONT_SIZE].lineHeight)];
        [_nameLabel setFont:[UIFont systemFontOfSize:NAMELABEL_FONT_SIZE]];
        [_nameLabel setTextColor:JXColor(51, 51, 51)];
        [self addSubview:_nameLabel];
        
        //4.添加时间
        _timeBtn = [[UIButton alloc]initWithFrame:CGRectMake(_nameLabel.x, CGRectGetMaxY(_headImage.frame)-TIMELABEL_FONT_SIZE-JXPublicInset/4, _nameLabel.width, TIMELABEL_FONT_SIZE)];
        [_timeBtn.titleLabel setFont:[UIFont systemFontOfSize:TIMELABEL_FONT_SIZE]];
        [_timeBtn setTitleColor:JXColor(102, 102, 102) forState:UIControlStateNormal];
        [_timeBtn setTitleEdgeInsets:UIEdgeInsetsMake(0, 5, 0, 0)];
        [_timeBtn setContentHorizontalAlignment:UIControlContentHorizontalAlignmentLeft];
        [_timeBtn setImage:[UIImage imageNamed:@"ic_time"] forState:UIControlStateNormal];
        [self addSubview:_timeBtn];
        
        //4.1添加用户性别
        _genderView = [[UIImageView alloc]init];
        [self addSubview:_genderView];
        
        //4.2添加楼主
        _authorView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"ic_comt_author"]];
        [_authorView setHidden:YES];
        [self addSubview:_authorView];
        
        //5.添加第1条分割线
        _spliteView1 = [[UIView alloc]initWithFrame:CGRectMake(_timeBtn.x, CGRectGetMaxY(_timeBtn.frame)+JXPublicInset, JXScreenW-CGRectGetMinX(_timeBtn.frame), 0.5)];
        [_spliteView1 setBackgroundColor:SPLITE_COLOR];
//        [self addSubview:_spliteView1];
        
        //6.添加内容lebel
        _contentLabel = [[UILabel alloc]initWithFrame:CGRectMake(_timeBtn.x, CGRectGetMaxY(_spliteView1.frame), JXScreenW- CGRectGetMinX(_timeBtn.frame)-JXPublicInset, CONTENTLABEL_FONT_SIZE)];
        [_contentLabel setFont:[UIFont systemFontOfSize:CONTENTLABEL_FONT_SIZE]];
        [_contentLabel setTextColor:JXColor(51, 51, 51)];
        [_contentLabel setNumberOfLines:0];
        [self addSubview:_contentLabel];
        
        //7.添加第2第分割线
        _spliteView2 =[[UIView alloc]initWithFrame:CGRectMake(_timeBtn.x, CGRectGetMaxY(_contentLabel.frame)+JXPublicInset*0.5, JXScreenW-CGRectGetMinX(_timeBtn.frame), 0.5)];
        [_spliteView2 setBackgroundColor:SPLITE_COLOR];
        [self addSubview:_spliteView2];
        
        //8.添加子评论的view
        _reComtView = [[ReComtView alloc]initWithWidth:JXScreenW-CGRectGetMinX(_nameLabel.frame)];
        [_reComtView setDelegate:self];
        [self addSubview:_reComtView];
        
        //9.添加查看更多的btn
        _moreBtn = [[UIButton alloc]init];
        [_moreBtn setContentHorizontalAlignment:UIControlContentHorizontalAlignmentRight];
        [_moreBtn setTitle:@"查看更多..." forState:UIControlStateNormal];
        [_moreBtn setTitleColor:JXThemeColor forState:UIControlStateNormal];
        [_moreBtn setHidden:YES];
        [_moreBtn.titleLabel setFont:[UIFont systemFontOfSize:12]];
        [self addSubview:_moreBtn];
    }
    return self;
}

-(void)setComtframe:(CommentViewFrame *)comtframe{
    _comtframe = comtframe;
    [self setFrame:comtframe.frame];
    [_headImage sd_setImageWithURL:[NSURL URLWithString:comtframe.comtInfo.headImage]];
    [_headImage setFrame:comtframe.headImageFrame];
    
    [_nameLabel setText:comtframe.comtInfo.commentName];
    [_nameLabel setFrame:comtframe.nameLabelFrame];
    
    //设置性别
    //默认为未知性别
    NSString *genderImg = @"ic_gender_unknow";
    //判断是男是女
    if ([[NSString stringWithFormat:@"%@",comtframe.comtInfo.commentGender] isEqualToString:@"1"]) {
        genderImg = @"ic_gender_man";
    }else if([[NSString stringWithFormat:@"%@",comtframe.comtInfo.commentGender] isEqualToString:@"2"]){
        genderImg = @"ic_gender_woman";
    }
    //判断是否是vip
    if (comtframe.comtInfo.commentLevel) {
        genderImg = [NSString stringWithFormat:@"%@_vip",genderImg];
    }
    [_genderView setImage:[UIImage imageNamed:genderImg]];
    [_genderView setSize:_genderView.image.size];
    [_genderView setX:CGRectGetMaxX(comtframe.nameLabelFrame)+5];
    [_genderView setY:_nameLabel.centerY-_genderView.height*0.5];
    
    //设置楼主
    if (![Utils isBlankString:self.authorId]) {
        if ([self.authorId isEqualToString:[NSString stringWithFormat:@"%@",comtframe.comtInfo.commentUserId]]) {
            [_authorView setHidden:NO];
            //调整位置
            [_authorView setX:CGRectGetMaxX(_genderView.frame)+5];
            [_authorView setY:_genderView.centerY-_authorView.height*0.5];
        }else{
            [_authorView setHidden:YES];
        }
    }else{
        [_authorView setHidden:YES];
    }
    
    [_timeBtn setTitle:comtframe.comtInfo.commentTime forState:UIControlStateNormal];
    [_timeBtn setFrame:comtframe.timeBtnFrame];
    
    [_replyImageBtn setFrame:comtframe.replyBtnFrame];
    [_replyImageBtn setTag:self.tag];
    [_replyImageBtn addTarget:self action:@selector(replyBtnClick:) forControlEvents:UIControlEventTouchUpInside];

    
    [_spliteView1 setFrame:comtframe.splite1Frame];
    [_contentLabel setText:comtframe.comtInfo.commentContent];
    [_contentLabel setFrame:comtframe.contentLabelFrame];
    
    if(comtframe.comtInfo.reComment == nil || comtframe.comtInfo.reComment.count==0){
        [_spliteView2 setHidden:YES];
        [_reComtView setHidden:YES];
    }else{
        [_spliteView2 setHidden:NO];
        [_spliteView2 setFrame:comtframe.splite2Frame];
        [_reComtView setHidden:NO];
        [_reComtView setReComtArray:comtframe.comtInfo.reComment];
        [_reComtView setFrame:comtframe.reComtViewFrame];
    }
    
    if(comtframe.comtInfo.isShowAll==0){
        [_moreBtn setHidden:NO];
        [_moreBtn setFrame:comtframe.moreBtnViewFrame];
        [_moreBtn addTarget:self action:@selector(moreBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    }else{
        [_moreBtn setHidden:YES];
        [_moreBtn setFrame:comtframe.moreBtnViewFrame];
    }
    
    
    if ([_delegate respondsToSelector:@selector(comtReplyType)]) {
        if ([[_delegate comtReplyType] isEqualToString:REPLY_TYPE_POSTSID]) {
            _headImage.userInteractionEnabled = YES;
            UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(headerImgClick:)];
            [_headImage addGestureRecognizer:tap];
        }
    }
}

-(void)headerImgClick:(UITapGestureRecognizer *)ges{
    
    if ([[NSString stringWithFormat:@"%@",_comtframe.comtInfo.commentUserId] isEqualToString:CURRENTUSERID]) {
        JXMinePostsCtl *ctl = [[JXMinePostsCtl alloc]initWithModelList:nil];
        [self.viewController.navigationController pushViewController:ctl animated:YES];
    }else{
        JXUserInfoCtl *ctl = [[JXUserInfoCtl alloc]initWithUserId:_comtframe.comtInfo.commentUserId];
        [self.viewController.navigationController pushViewController:ctl animated:YES];
    }
}
- (void)moreBtnClick:(UIButton *)btn{
    if([self.delegate respondsToSelector:@selector(moreBtnClick:)]){
        [_delegate moreBtnClick:self.tag];
    }
}

-(void)replyBtnClick:(UIButton *)btn{
    if ([self.delegate respondsToSelector:@selector(replyBtnClick:)]) {
        //显示JXComtPopMenu
        _menu = [[JXComtPopMenu alloc]initHaveArrow:NO];
        [_menu setTag:self.tag];
        [_menu setDelegate:self];
        UIWindow * window=[[[UIApplication sharedApplication] delegate] window];
        CGRect rect=[btn convertRect:btn.bounds toView:window];
        [_menu showInPoint:CGPointMake(CGRectGetMinX(rect)-_menu.container.width-5, rect.origin.y+rect.size.height*0.5-_menu.container.height*0.5)];
        [btn setSelected:YES];
    }
}

-(void)childViewClick:(NSInteger)index{
    if([self.delegate respondsToSelector:@selector(childReplyItemClick:index:)]){
        [_delegate childReplyItemClick:self.tag index:index];
    }
}

-(void)chatBtnClick:(NSInteger)index{
    if([self.delegate respondsToSelector:@selector(childChatItemClick:index:)]){
        [_delegate childChatItemClick:self.tag index:index];
    }
}

#pragma mark JXComtPopMenu代理方法
-(void)popMenuDidDismissed:(JXComtPopMenu *)popMenu{
    [_replyImageBtn setSelected:NO];
}

-(void)popMenuReplyBtnClick:(UIButton *)btn{
    if ([self.delegate respondsToSelector:@selector(replyBtnClick:)]) {
        [_delegate replyBtnClick:btn];
    }
}

-(void)popMenuChatBtnClick:(UIButton *)btn{
    if (!TOKEN) {
        JXNavigationController *nav = [[JXNavigationController alloc]initWithRootViewController:[[JXLoginRegistCheckController alloc]init]];
        [self.viewController presentViewController:nav animated:YES completion:^{}];
        return;
    }
    
    if ([self.delegate respondsToSelector:@selector(chatBtnClick:)]) {
        [_delegate chatBtnClick:btn];
    }
}

@end
